/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddSlaveDialog.java
 *
 * Created on 20 déc. 2008, 12:07:28
 */

package modbuspal.slave;

import java.awt.Frame;
import modbuspal.toolkit.GUITools;
import modbuspal.main.ModbusConst;
import modbuspal.toolkit.NumericTextField;

/**
 *
 * @author nnovic
 */
class AddRegistersDialog
extends javax.swing.JDialog
implements ModbusConst
{
    private boolean added = false;
    private int lowValue = 1;
    private int highValue = 65536;
    private int implementation = IMPLEMENTATION_MODBUS;

    /** Creates new form AddSlaveDialog */
    public AddRegistersDialog(Frame frame, ModbusSlaveDialog parent)
    {
        super(frame, true);

        implementation = parent.getModbusSlave().getImplementation();
        switch( implementation )
        {
            default:
            case IMPLEMENTATION_MODBUS:
                lowValue = 1;
                highValue = 65536;
                break;

            case IMPLEMENTATION_JBUS:
                lowValue = 0;
                highValue = 65535;
                break;
        }

        initComponents();
        GUITools.align(parent,this);
    }

    public boolean isAdded()
    {
        return added;
    }

    public int getStartingAddress()
    {
        String from = fromTextField.getText();
        int addr = Integer.parseInt(from);
        switch(implementation)
        {
            default:
            case IMPLEMENTATION_MODBUS:
                return addr-1;
            case IMPLEMENTATION_JBUS:
                return addr;
        }
    }

    public int getQuantity()
    {
        int start = Integer.parseInt( fromTextField.getText() );
        int end = Integer.parseInt( toTextField.getText() );

        return end-start+1;
    }

    /** This method is called getStartingAddress within the constructor getQuantity
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        addButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        toTextField = new NumericTextField();
        fromTextField = new NumericTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        getContentPane().setLayout(new java.awt.GridBagLayout());

        addButton.setText("Add");
        addButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 2, 10, 10);
        getContentPane().add(addButton, gridBagConstraints);

        cancelButton.setText("Cancel");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 2;
        gridBagConstraints.insets = new java.awt.Insets(10, 2, 10, 2);
        getContentPane().add(cancelButton, gridBagConstraints);

        jLabel1.setText("From:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 10, 2, 2);
        getContentPane().add(jLabel1, gridBagConstraints);

        jLabel2.setText("To:");
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 10, 10, 2);
        getContentPane().add(jLabel2, gridBagConstraints);

        toTextField.setText(String.valueOf(highValue));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(2, 2, 10, 10);
        getContentPane().add(toTextField, gridBagConstraints);

        fromTextField.setText(String.valueOf(lowValue));
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.HORIZONTAL;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
        gridBagConstraints.insets = new java.awt.Insets(10, 2, 2, 10);
        getContentPane().add(fromTextField, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addButtonActionPerformed
        added = true;
        setVisible(false);
    }//GEN-LAST:event_addButtonActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        added = false;
        setVisible(false);
    }//GEN-LAST:event_cancelButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addButton;
    private javax.swing.JButton cancelButton;
    private javax.swing.JTextField fromTextField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JTextField toTextField;
    // End of variables declaration//GEN-END:variables

}
